<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %>
<%@ page import="javax.naming.Context"%> 
<%@ page import="javax.sql.DataSource"%> 
<%@ page import="javax.naming.InitialContext"%> 
<%@ page import="java.sql.*"%> 
<%@page import="dataconn.dataconn;"%> 
<%
     request.setCharacterEncoding("utf-8");
     dataconn con=new dataconn();
     String New_GroupName=request.getParameter("groupname");
     String sql0="select GroupName from TestGroup where  GroupName='"+New_GroupName+"'";
     ResultSet rs0=con.executeQuery(sql0);
     if(rs0.next())
     {
     out.print("<br>该分组已经存在！<br>");
     out.print("<a href='AddGroup.jsp'>重新添加</a>");
     return;}
     String sql="insert into TestGroup(GroupName,GroupOwnerID,AddedByID)values('"+New_GroupName+"','1','1')";
     con.executeUpdate(sql);
     String Users[]=request.getParameterValues("User2");
     String Projects[]=request.getParameterValues("Project2");
     String sql1="select top 1 GroupID from TestGroup order by GroupID desc";
     ResultSet rs=con.executeQuery(sql1);
     rs.next();
     String GroupID=rs.getString("GroupID");
     for(int i=0;i<Users.length;i++)
     {
        String sql4="select UserID from TestUser where UserName='"+Users[i]+"'";
        ResultSet rs2=con.executeQuery(sql4);
        rs2.next();
        String UserID=rs2.getString("UserID");
        String sql5="insert Group_User(GroupID,UserID,AddedByID) values('"+GroupID+"','"+UserID+"','1')";
        con.executeUpdate(sql5);
        rs2.close();
     }
          for(int j=0;j<Projects.length;j++)
     {
        String sql2="select ProjectID from TestProject where ProjectName='"+Projects[j]+"'";
        ResultSet rs1=con.executeQuery(sql2);
        rs1.next();
        String ProjectID=rs1.getString("ProjectID");
        String sql3="insert Group_Project(GroupID,ProjectID,AddedByID) values('"+GroupID+"','"+ProjectID+"','1')";
        con.executeUpdate(sql3);
        rs1.close();
     }

     rs.close();
     con.closeConn();
     con.closeStmt();
     response.sendRedirect("AddGroup.jsp");
 %>
 
